package com.swordget.service.impl;

import com.swordget.dao.PanelDao;
import com.swordget.entity.po.PanelPo;
import com.swordget.entity.vo.PanelVo;
import com.swordget.service.PanelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: David
 * @Date: Created in 11:44 2021/2/18
 * @Description:
 * @Modified By:
 * @Version:
 */
@Service
public class PanelServiceImpl implements PanelService {

    private PanelDao panelDao;

    @Autowired
    public PanelServiceImpl(PanelDao panelDao) {
        this.panelDao = panelDao;
    }

    private final static Logger LOG = LoggerFactory.getLogger(PanelServiceImpl.class);

    @Override
    public boolean addPanel(String paneName, String loginId) {
        //id
        String id = String.valueOf(System.currentTimeMillis());
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String createTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
        return panelDao.savePanel(id, paneName, createDate, createTime, loginId);
    }

    @Override
    public List<PanelVo> showPanelList() {

        List<PanelPo> panelPoList = panelDao.showPanelList();
        List<PanelVo> panelVoList = panelPoList.stream()
                .map(p -> {
                    return PanelVo.builder()
                            .id(p.getId())
                            .panelName(p.getPanelName())
                            .createDate(p.getCreateDate())
                            .createTime(p.getCreateTime())
                            .createUser(p.getCreateUser())
                            .build();
                }).collect(Collectors.toList());

        return panelVoList;
    }


    @Override
    public List<Map<String, String>> showTablePanel(String panelId) {
        return panelDao.showTablePanel(panelId);
    }

    @Override
    public boolean deletePanel(String panelId) {
        return panelDao.deletePanel(panelId);
    }

    @Override
    public boolean modifyPanelMenu(PanelVo panelVo) {

        return panelDao.modifyPanelMenu(panelVo);
    }

    @Override
    public boolean addPanelTable(String tableId, String panelId) {
        return panelDao.addPanelTable(tableId, panelId);
    }

    @Override
    public List<Map<String, Object>> getCount(String panelId, String tableNa, String tableCn) {
        //获取面板下所有表信息
        List<Map<String, String>> maps = panelDao.showTablePanelByTableCn(panelId, tableNa, tableCn);

        //记录所有统计数据
        List<Map<String, Object>> countMap = new ArrayList<>(maps.size());

        for (int i = 0; i < maps.size(); i++) {
            try {
                Map<String, String> map = maps.get(i);
                //获取这张表的统计数据
                countMap.add(panelDao.getCount(map));
            } catch (Exception e) {
                LOG.error("error", e);
            }
        }
        return countMap;

    }


}
